package com.misolab.android.post.activity;

import java.io.StringReader;
import java.net.URLDecoder;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;

import com.misolab.android.http.HttpClient;
import com.misolab.android.http.HttpClient.HttpClientListener;
import com.misolab.android.post.R;
import com.misolab.android.post.util.parse.xml.XMLParser;

public class PostActivity extends Activity {
	
	String XML = "<A><B param='1'>TEST</B></A>";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//        testParse();
        testHttp();
    }
    
    private void testHttp() {
    	
    	String uri = "http://kih.woobi.co.kr/post/joinCheck.php?ID=admin";
    	
    	HttpClient hc = new HttpClient(new HttpClientListener() {
			
			@Override
			public void onSuccess(HttpClient httpClient, String content) {
				System.out.println("content:"+content);
				
				try{
					String resData = URLDecoder.decode(content);
					
					JSONObject obj = new JSONObject(resData);
//					obj.getString(name);
//					JSONArray arrayObj = obj.getJSONArray("ITEMS");
//					arrayObj.get(index);
					System.out.println(obj.toString());
					System.out.println("MESSAGE : "+ obj.getString("MESSAGE"));
					
				}catch (Exception e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(HttpClient httpClient, Throwable error) {
				System.out.println("onFailure");
			}
		});
		hc.get(uri);
	}

	void testParse(){
    	
    	try {
        	InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(XML));
        	
        	
        	SAXParserFactory spf = SAXParserFactory.newInstance();
        	SAXParser sp = spf.newSAXParser();
        	XMLReader xr = sp.getXMLReader();

        	XMLParser parse = new XMLParser();
        	xr.setContentHandler(parse);
        	xr.parse(is);		
        	
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    	
        
    }
}